//
//  ProtectBasicDocumentViewController.m
//  MyProject
//
//  Created by 石小强 on 2018/5/8.
//  Copyright © 2018年 yanjiaming. All rights reserved.
//

#import "ProtectBasicDocumentViewController.h"
#import "UITextView+Placeholder.h"
#import "EntryProtectTargetPositionVC.h"
#import <AlipaySDK/AlipaySDK.h>
#import "APAuthV2Info.h"
#import "DataSigner.h"
#import "VitaeVC.h"
#import "NSString+Expand.h"

@interface ProtectBasicDocumentViewController ()

@end

@implementation ProtectBasicDocumentViewController {
    NSString *_positionId;
    NSString *_positionName;
    NSString *_companyName;
    NSString *_minSalary;
    NSString *_maxSalary;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.title = @"提交申请";
    self.textView.placeholder = @"详细的自我描述和问题描述有助于同事有的放矢，更加有针对性的为你提供服务。";
}

/** 目标职位按钮 */
- (IBAction)targetBtnClick:(id)sender {
    EntryProtectTargetPositionVC *position = [[EntryProtectTargetPositionVC alloc] init];
    position.companyId = self.companyId;
    WS(weakSelf)
    position.okBlock = ^(NSString *positionId, NSString *positionName, NSString *companyName, NSString *minSalary, NSString *maxSalary) {
        _positionId = positionId;
        _positionName = positionName;
        _companyName = companyName;
        _minSalary = minSalary;
        _maxSalary = maxSalary;
        weakSelf.targetPositionL.text = _positionName;
    };
    [self.navigationController pushViewController:position animated:YES];
}

#pragma mark - 个人简历按钮点击事件
/** 个人简历按钮 */
- (IBAction)resumeBtnClick:(id)sender {
    VitaeVC *vc = [[VitaeVC alloc]init];
    vc.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:vc animated:YES];
}

#pragma mark - 芝麻信用分按钮点击事件
/** 芝麻信用分按钮 */
- (IBAction)creditBtnClick:(id)sender {
    [self showProgressWithView:self.view animated:YES];
    [YJMSendRequest postDataWithParameters:@{} url:@"common/getAlipayAccountAuthSign" result:^(NSDictionary *result, NSError *error) {
        result = [CommonUtils decryptDicWithResult:result];
        [self hideProgress:self.view animated:YES];
        if([result[@"result"] integerValue] == 1)
        {
         NSString *authInfoStr = result[@"data"];
            authInfoStr = [authInfoStr stringByReplacingOccurrencesOfString:@"\"" withString:@""];
            [[AlipaySDK defaultService] auth_V2WithInfo:authInfoStr
                                             fromScheme:@"taijieAlipay"
                                               callback:^(NSDictionary *resultDic) {
                                                   NSLog(@"result = %@",resultDic);
                                                   // 解析 auth code
                                                   NSString *result = resultDic[@"result"];
                                                   NSString *authCode = nil;
                                                   if (result.length>0) {
                                                       NSArray *resultArr = [result componentsSeparatedByString:@"&"];
                                                       for (NSString *subResult in resultArr) {
                                                           if (subResult.length > 10 && [subResult hasPrefix:@"auth_code="]) {
                                                               authCode = [subResult substringFromIndex:10];
                                                               break;
                                                           }
                                                       }
                                                   }
                                                   NSLog(@"授权结果 authCode = %@", authCode?:@"");
                                                   [self getZMScoreWith:authCode?:@""];
                                      
                                               }];

        }else{
            [CommonUtils showToast:[result objectForKey:@"msg"]];
        }
        
    }];


}
- (void)getZMScoreWith:(NSString *)authCode {
    WS(weakSelf)
    [self showProgressWithView:self.view animated:YES];
    [SendRequest getZmScoreByToken:authCode result:^(NSDictionary *result, NSError *error) {
        [weakSelf hideProgress:self.view animated:YES];
        result = [CommonUtils decryptDicWithResult:result];
        if([result[@"result"] integerValue] == 1) {
            NSString *codeStr = result[@"data"];
            codeStr = [codeStr stringByReplacingOccurrencesOfString:@"\"" withString:@""];
            weakSelf.creditL.text = [NSString stringWithFormat:@"%@",codeStr];
        } else {
            [CommonUtils showToast:[result objectForKey:@"msg"]];
        }
    }];
}
/** 底部确认按钮 */
- (IBAction)okBtnClick:(id)sender {
    if (!_positionName || _positionName.length == 0) {
        [CommonUtils showToast:@"请选择目标职位"];
        return;
    }
    if (!self.textView.text && self.textView.text.length == 0) {
        [CommonUtils showToast:@"请给对方留言"];
        return;
    }
    if (self.textView.text.length < 20 || self.textView.text.length > 500) {
        [CommonUtils showToast:@"留言的长度为20-500个字"];
        return;
    }
    
    NSMutableDictionary *params = [NSMutableDictionary dictionary];
    params[@"incumbentAssurePassId"] = self.assurePassId;
#warning 芝麻信用得分 --- 暂时写死
    params[@"zhimaCreditScore"] = [NSString stringWithFormat:@"%zd",[self.creditL.text integerValue]];
    if (_positionId && _positionId.length > 0) {
        params[@"positionId"] = _positionId;
    }
    params[@"positionName"] = _positionName;
    params[@"companyName"] = _companyName;
    params[@"minSalary"] = _minSalary;
    params[@"maxSalary"] = _maxSalary;
    params[@"leaveMesseges"] = self.textView.text;
    
    WS(weakSelf)
    [self showProgressWithView:self.view animated:YES];
    [SendRequest orderAssurePassWithParam:params result:^(NSDictionary *result, NSError *error) {
        [weakSelf hideProgress:weakSelf.view animated:YES];
        result = [CommonUtils decryptDicWithResult:result];
        if([result[@"result"] integerValue] == 1) {
            SaintiLog(@"预约保过 = %@", [result objectForKey:@"data"]);
            [weakSelf.navigationController popViewControllerAnimated:YES];
        } else {
            [CommonUtils showToast:[result objectForKey:@"msg"]];
        }
    }];
}


#pragma mark - UITextViewDelegate

- (void)textViewDidChange:(UITextView *)textView {
    if (textView.markedTextRange == nil) {
        NSLog(@"text:%@", textView.text);
        if (textView.text.length > 500) {
            ALERT_VIEW(@"最多输入500个字");
            textView.text = [textView.text subString:0 length:500];
        }
    }
}

@end
